import request from '@/utils/request'
import { getStorage } from '@/utils/storage'
export * from './Article'
// 登录接口
export const loginAPI = (mobile, code) => request({
  url: '/v1_0/authorizations',
  method: 'POST',
  data: {
    mobile,
    code
  }
})
// 获取用户的频道
export const getUserChannelAPI = () => request({
  url: '/v1_0/user/channels'
})

// 获取所有的频道
export const getAllChannelAPI = () => request({
  url: '/v1_0/channels'
})

// 编辑用户频道的API
export const setChannelAPI = (channels) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels
  }
})
// 删除指定用户频道
export const delChannelAPI = (id) => request({
  url: '/v1_0/user/channels/' + id,
  method: 'DELETE'
})
// 联想词汇的API
export const suggestionAPI = (searchStr) => request({
  url: '/v1_0/suggestion',
  params: {
    q: searchStr
  }
})
// 获取搜索结果的API
export const searchAPI = ({ searchStr, page = 1, per_page = 10 }) => request({
  url: '/v1_0/search',
  params: {
    q: searchStr,
    page,
    per_page
  }
})

// 关注用户的API
export const followAPI = (id) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: id
  }
})
// 取消用户的API
export const unFollowAPI = (id) => request({
  url: '/v1_0/user/followings/' + id,
  method: 'DELETE'
})

// 取消用户点赞的API
export const unLinkingAPI = (id) => request({
  url: '/v1_0/article/likings/' + id,
  method: 'DELETE'
})

// 对评论点赞的API
export const likCommentAPI = (id) => request({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: id
  }
})
// 取消评论点赞的API
export const unlikCommentAPI = (id) => request({
  url: '/v1_0/comment/likings/' + id,
  method: 'DELETE'
})

// 对文章或者评论进行评论
export const setComment = ({ target, content, art_id = null }) => {
  const obj = {
    target,
    content
  }
  if (target.length !== 0) {
    obj[art_id] = art_id
  }
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}

// 获取用户自己的信息
export const getUserAPI = () => request({
  url: '/v1_0/user'
})

// 编辑用户个人资料
export const editUserAPI = () => request({
  url: '/v1_0/user/profile'
})

// 编辑用户照片资料（头像、身份证照片）
export const editPhoneAPI = (fd) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  data: fd
})

// 编辑用户信息的API
export const editMessageAPI = (obj) => request({
  url: '/v1_0/user/profile',
  method: 'PATCH',
  data: obj
})

// 续签token
export const newTokenAPI = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: 'Bearer ' + getStorage('refresh_token')
  }
})
